package main

import (
	"flag"
	"fmt"
	"jvm/classpath"
	"jvm/rtda/heap"
	"strings"
)

func main() {
	cmd := parseCmd()
	if cmd.versionFlag {
		printVersionInfo()
	} else if cmd.helpFlag || cmd.class == "" {
		flag.Usage()
	} else {
		startJVM(cmd)
	}

}
func startJVM(cmd *Cmd) {
	classpath := classpath.NewClassPath(cmd.XjreOption, cmd.cpOption)
	loader := heap.NewClassLoader(classpath, cmd.verboseClassFlag)
	className := strings.Replace(cmd.class, ".", "/", -1)
	class := loader.LoadClass(className)
	mainMethod := getMainMethodclass(class)
	if mainMethod == nil {
		fmt.Printf("Main method not found in class %s\n", cmd.class)
	} else {
		interpret(mainMethod, cmd.verboseInstFlag)
	}

}
func getMainMethodclass(class *heap.Class) *heap.Method {
	for _, method := range class.Methods() {
		if method.Name() == "main" {
			return &method
		}
	}
	return nil
}

func printVersionInfo() {
	fmt.Println("java version 1.8")
	fmt.Println("JVM MADE BY CYL")
}
